package statistics;

import java.util.Random;

/**
 * This class represents an UniformDistribution.
 */

public class UniformDistribution extends ContinuousDistribution {

  private double a;
  private double b;

  public UniformDistribution(double a,double b) {
    this.a = a;
    this.b = b;
    random = new Random();
  }

  /**
   * Constructs an Uniform Distribution on [a,b]. You must also specify the
   * random seed that will be used to realize random variables with this distribution.
   * @param a first parameter.
   * @param b second parameter
   * @param random the random seed.
   */

  public UniformDistribution(double a,double b, Random random) {
    this.a = a;
    this.b = b;
    this.random = random;
  }

  /**
   * Returns the expectation of an uniform distribution with parameters a and b.
   */

  public double expectation() {
    return (a+b)/2;
  }

  /**
   * Returns the variance of an uniform distribution with parameters a and b.
   */

  public double variance() {
    return (b-a)*(b-a)/(12);
  }

  /**
   * Returns P(X <= k), X is a random variable with an Uniform distribution.
   * @param k
   */

  public double probabilityFunction(double x) {
    if (x <= a) return 0;
      else if (x > b) return 0;
      else return (x-a)/(b-a);
  }

  /**
   * Returns a realization of a random variable with an Uniform distribution.
   */

  public double nextRandom() {
    return a + (b - a)*random.nextDouble();
  }

}
